package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.web.api.domain.TChapterCatagory;
import com.ruoyi.web.api.domain.TChapterCatagoryVo;
import com.ruoyi.web.service.ITChapterCatagoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章分类Controller
 * 
 * @author ruoyi
 * @date 2023-10-08
 */
@RestController
@RequestMapping("/catagory")
public class TChapterCatagoryController extends BaseController
{
    @Autowired
    private ITChapterCatagoryService tChapterCatagoryService;

    @GetMapping("/all")
    public R<List<TChapterCatagory>> all()
    {
        List<TChapterCatagory> list = tChapterCatagoryService.selectTChapterCatagoryList(null);
        return R.ok(list);
    }


    @GetMapping("/getCategoryAndChapters")
    public R<List<TChapterCatagoryVo>> getCategoryAndChapters()
    {
        List<TChapterCatagoryVo> list = tChapterCatagoryService.selectTChapterCatagoryAndChapters();
        return R.ok(list);
    }


    /**
     * 查询文章分类列表
     */
//    @RequiresPermissions("web:catagory:list")
    @GetMapping("/list")
    public TableDataInfo list(TChapterCatagory tChapterCatagory)
    {
        startPage();
        List<TChapterCatagory> list = tChapterCatagoryService.selectTChapterCatagoryList(tChapterCatagory);
        return getDataTable(list);
    }

    /**
     * 导出文章分类列表
     */
    @RequiresPermissions("web:catagory:export")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TChapterCatagory tChapterCatagory)
    {
        List<TChapterCatagory> list = tChapterCatagoryService.selectTChapterCatagoryList(tChapterCatagory);
        ExcelUtil<TChapterCatagory> util = new ExcelUtil<TChapterCatagory>(TChapterCatagory.class);
        util.exportExcel(response, list, "文章分类数据");
    }

    /**
     * 获取文章分类详细信息
     */
    @RequiresPermissions("web:catagory:query")
    @GetMapping(value = "/{ccid}")
    public AjaxResult getInfo(@PathVariable("ccid") Long ccid)
    {
        return success(tChapterCatagoryService.selectTChapterCatagoryByCcid(ccid));
    }

    /**
     * 新增文章分类
     */
    @RequiresPermissions("web:catagory:add")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TChapterCatagory tChapterCatagory)
    {
        return toAjax(tChapterCatagoryService.insertTChapterCatagory(tChapterCatagory));
    }

    /**
     * 修改文章分类
     */
    @RequiresPermissions("web:catagory:edit")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TChapterCatagory tChapterCatagory)
    {
        return toAjax(tChapterCatagoryService.updateTChapterCatagory(tChapterCatagory));
    }

    /**
     * 删除文章分类
     */
    @RequiresPermissions("web:catagory:remove")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ccids}")
    public AjaxResult remove(@PathVariable Long[] ccids)
    {
        return toAjax(tChapterCatagoryService.deleteTChapterCatagoryByCcids(ccids));
    }
}
